package org.example.controller;

import org.example.GetConnection;
import org.example.info.Label;
import org.example.info.PageSize;
import org.example.info.Result;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class LabelController {
    private Connection connection = new GetConnection().GetConnection();
    private PreparedStatement statement;
    private ResultSet resultSet;

    @GetMapping("/label")
    public Result<?> getLabel() throws SQLException {
        statement = connection.prepareStatement("select * from label");
        resultSet = statement.executeQuery();
        List<Label> labelList = new ArrayList<>();
        while (resultSet.next()) {
            labelList.add(new Label(resultSet.getInt("id"), resultSet.getString("introduce"), resultSet.getString("name")));
            System.out.println(resultSet.getString("name"));
        }
        return Result.success(labelList);
    }

    @GetMapping("/label/search/{key}")
    public Result<?> searchLabel(@PathVariable String key) throws SQLException {
        System.out.println(key);
        statement = connection.prepareStatement("select * from label where name like ?");
        statement.setString(1,"%"+key+"%");
        List<Label> labelList = new ArrayList<>();
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            labelList.add(new Label(resultSet.getInt("id"), resultSet.getString("introduce"), resultSet.getString("name")));
        }
        return Result.success(labelList);
    }


    @PostMapping("/label")
    public Result<?> addLabel(@RequestBody Label label) throws SQLException {
        System.out.println(label);
        statement = connection.prepareStatement("insert into label(name,introduce) values (?,?)");
        statement.setString(1,label.getName());
        statement.setString(2,label.getIntroduce());
        statement.executeUpdate();
        return  Result.success();
    }

    @PutMapping("/label")
    public Result<?> updateLabel(@RequestBody Label label) throws SQLException {
        System.out.println(label);
        // set name = name +1
        // 多个 set，注意逗号分隔
        statement = connection.prepareStatement("update label set name = ?,introduce =? where id = ?");
        statement.setString(1,label.getName());
        statement.setString(2,label.getIntroduce());
        statement.setInt(3,label.getId());
        statement.executeUpdate();
        return  Result.success();
    }

    @DeleteMapping("/label")
    public Result<?> deleteLabel(@RequestBody Label label) throws SQLException {
        System.out.println(label);
        statement = connection.prepareStatement("delete from label where id = ?");
        statement.setInt(1,label.getId());
        statement.executeUpdate();
        return Result.success();
    }

}
